package com.study.security.config;

import com.study.security.service.MemoryUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;

/**
 * Security配置Bean
 *
 * @Author YL
 * @Create 2024/3/30 10:48
 * @Version 1.0
 */
@Configuration
public class SecurityBeanConfig {

    /**
     * 会话注册表
     *
     * @return
     */
    @Bean
    public SessionRegistryImpl createSessionRegistry() {
        return new SessionRegistryImpl();
    }

    /**
     * 配置认证信息token存储位置（内存）
     *
     * @return
     */
    @Bean
    public InMemoryTokenRepositoryImpl createInMemoryTokenRepositoryImpl() {
        return new InMemoryTokenRepositoryImpl();
    }

    /**
     * 配置认证信息token存储位置（数据库）
     *
     * @return
     */
    //@Bean
//    public JdbcTokenRepositoryImpl createJdbcTokenRepositoryImpl(DataSource dataSource) {
//        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
//        jdbcTokenRepository.setDataSource(dataSource);
//        jdbcTokenRepository.setCreateTableOnStartup(true); // 初始化创建存储token的表
//        return jdbcTokenRepository;
//    }

    /**
     * 密码加密方式
     * 在哪里配置到security参考：InitializeUserDetailsManagerConfigurer
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 用户信息查询service
     * 在哪里配置到security参考：InitializeUserDetailsManagerConfigurer
     *
     * @return
     */
    @Bean
    public UserDetailsService userDetailsService() {
        return new MemoryUserDetailsService();
    }
}
